package android.concisapp.parser;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.util.Log;

public class ParserCalendarList {

	private CalendarItem item;
	private ArrayList<CalendarItem> arrayItem;

	public ParserCalendarList() {
		try {
			Log.i("FEED TASK", "BEGIN");
			URL url = new URL(
					"https://www.google.com/calendar/feeds/2fs98kspp7n30prf8vvmbnh33c%40group.calendar.google.com/public/basic");

			XmlPullParserFactory factory = XmlPullParserFactory.newInstance();

			factory.setNamespaceAware(false);
			XmlPullParser xpp = factory.newPullParser();
			xpp.setInput(url.openConnection().getInputStream(), "UTF_8");

			boolean insideItem = false;

			int eventType = xpp.getEventType();
			arrayItem = new ArrayList<CalendarItem>();
			item = new CalendarItem();
			while (eventType != XmlPullParser.END_DOCUMENT) {
				if (eventType == XmlPullParser.START_TAG) {
					if (xpp.getName().equalsIgnoreCase("entry")) {
						Log.d("FEED ITEM!", "PASA");
						insideItem = true;
					} else if (xpp.getName().equalsIgnoreCase("title")) {
						if (insideItem) {
							item.setTitle(xpp.nextText());
							Log.i("FEED TITLE!", item.getTitle());
						}
					} else if (xpp.getName().equalsIgnoreCase("content")) {
						if (insideItem) {
							String total = xpp.nextText();
							String date = total.substring(
									total.indexOf("Cuándo:") + 12,
									total.indexOf("Lugar:"));
//							Log.i("DATE!", date);
							formatDate(date);
							String place = total.substring(
									total.indexOf("Lugar:") + 7,
									total.indexOf("Estado del evento:"));
							Log.i("PLACE!", place);
							// item.setDate(null);
							// item.setPlace(null);
							Log.i("FEED DESCRIPTION!", item.getDate() + " "
									+ item.getPlace());
						}
					}
				} else if (eventType == XmlPullParser.END_TAG
						&& xpp.getName().equalsIgnoreCase("entry")) {
					insideItem = false;
					arrayItem.add(item);
					item = new CalendarItem();
				}
				eventType = xpp.next(); // move to next element
			}
			Log.i("FEED RESULT!", "tamaño " + arrayItem.size());

		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public ArrayList<CalendarItem> getArrayItem() {
		return arrayItem;
	}

	public void setArrayItem(ArrayList<CalendarItem> arrayItem) {
		this.arrayItem = arrayItem;
	}

	public String formatDate(String date) {
		String result = "";
		String day = date.substring(0, 2);
		String month = date.substring(6, 9);
		String year = date.substring(13, 17);
		result = day + "/" + month + "/" + year;
		Log.i("RES", result);
		return result;
	}
}
